import {
    reactive, ref,Ref
} from 'vue';
import { useRouter } from 'vue-router';
import store from 'src/store';
import BetterUtilTools from 'better-util-tools';


const UtilTools = new BetterUtilTools();
export default (obj:any) => {
    const router = useRouter();
    const toLogin = () => {
        router.push('/login');
    };


    // 初始化分页对象
    const pager = reactive({
        'pageNo': 1,
        'pageSize': 10,
        'total': 0
    });
    const tableData = ref([]);


    const {tagData} = store.state;
    // 获取用户列表
    const getData = async () => {
        let params:paramsObj = {
            'pageNo': pager.pageNo,
            'pageSize': pager.pageSize
        };

        if (obj.type === 'article') {
            params.self = true;
            params.id = obj.searchItem.id;
        }
        obj['getDataFn']({
            'data': params
        })
            .then((res: any) => {
                if (obj.type === 'article') {
                    res.data.articleDatas.forEach((item:articleObj) => {
                        item.createTime = UtilTools.formatDateTime({
                            'date': item.createTime
                        });
                        item.modifyTime = UtilTools.formatDateTime({
                            'date': item.modifyTime
                        });
                        item.tagName = tagData.filter(
                            tag => tag.id === item.tagId
                        )[0]?.value;
                    });
                    tableData.value = res.data.articleDatas;
                } else {
                    res.data.Datas.forEach((item:replyObj) => {
                        item.createTime = UtilTools.formatDateTime({
                            'date': item.createTime
                        });
                    });
                    tableData.value = res.data.Datas;
                }
                pager.total = res.data.total;
            })
            .catch((e: any) => {
                console.log(e);
            });
    };
    const routerPush = (id: number, routerName: string) => {
        let params = {
            'name': routerName,
            'params': {
                'topicId': id
            },
            'query':{
                'self': obj.self
            }
        };
        router.push(params);
    };
    //切换页面大小方法
    const handleSizeChange = (val: number) => {
        pager.pageSize = val;
        pager.pageNo = 1;
        getData();
    }; //分页方法
    const handleCurrentChange = (val: number) => {
        pager.pageNo = val;
        getData();
    };
    return {
        router,
        pager,
        tableData,
        handleCurrentChange,
        handleSizeChange,
        toLogin,
        getData,
        routerPush
    };
};


